<?php

App::uses('AppController', 'Controller');

/**

 * Cities Controller

 *

 * @property City $City

 * @property PaginatorComponent $Paginator

 */

class CitiesController extends AppController {

	public $components = array('Paginator');


	public function admin_index() {
		$this->City->recursive = 0;
		$this->set('cities', $this->Paginator->paginate());
	}

 	public function admin_view($id = null) {
 		if (!$this->City->exists($id)) {
 			throw new NotFoundException(__('Invalid city'));
 		}
 		$options = array('conditions' => array('City.' . $this->City->primaryKey => $id));
 		$this->set('city', $this->City->find('first', $options));
 	}

 	public function admin_add() {
 		if ($this->request->is('post')) {
 			$this->City->create();
 			if ($this->City->save($this->request->data)) {
 				$this->Session->setFlash(__('The city has been saved.'), 'default', array('class' => 'alert alert-success'));
 			} else {
 				$this->Session->setFlash(__('The city could not be saved. Please, try again.'), 'error', array('class' => 'alert alert-danger'));
 			}
 		}
 	}

 	public function admin_edit($id = null) {
 		if (!$this->City->exists($id)) {
 			throw new NotFoundException(__('Invalid city'));
 		}
 		if ($this->request->is(array('post', 'put'))) {
 			if ($this->City->save($this->request->data)) {
 				$this->Session->setFlash(__('The city has been saved.'), 'default', array('class' => 'alert alert-success'));
 				return $this->redirect(array('action' => 'index'));
 			} else {
 				$this->Session->setFlash(__('The city could not be saved. Please, try again.'), 'default', array('class' => 'alert alert-danger'));
 			}
 		} else {
 			$options = array('conditions' => array('City.' . $this->City->primaryKey => $id));
 			$this->request->data = $this->City->find('first', $options);
 		}
 	}

 	public function admin_delete($id = null) {
 		$this->City->id = $id;
 		if (!$this->City->exists()) {
 			throw new NotFoundException(__('Invalid city'));
 		}
 		$this->request->onlyAllow('post', 'delete');
 		if ($this->City->delete()) {
 			$this->Session->setFlash(__('The city has been deleted.'), 'default', array('class' => 'alert alert-success'));
 		} else {
 			$this->Session->setFlash(__('The city could not be deleted. Please, try again.'), 'default', array('class' => 'alert alert-danger'));
 		}
 		return $this->redirect(array('action' => 'index'));
	}

}

